﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;

namespace Infrastructure.Aspect.Validation.Wcf
{
    public class ValidationBehavior : IEndpointBehavior
    {

        private bool Enabled = false;
        public  ValidationBehavior(bool enabled)
        {
            this.Enabled = enabled; 
 
        }
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            if (!this.Enabled)
            {
                return;
            }
            else
            {
                foreach (var clientOperation in clientRuntime.Operations)
                {
                    clientOperation.ParameterInspectors.Add(new SqlInjectParameterInspector());                    
                }
            }
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            if (!this.Enabled)
                return;
            foreach (var item in endpointDispatcher.DispatchRuntime.Operations)
            {
                item.ParameterInspectors.Add(new SqlInjectParameterInspector());

                
            }


        }

        public void Validate(ServiceEndpoint endpoint)
        {
            
        }
    }
}
